<!DOCTYPE HTML>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<meta charset="utf-8">
<head th:include="include :: header"></head>
<body class="gray-bg">
    
     <div class="container-div">
		<div class="row">
			<div class="col-sm-12 search-collapse">
				<form id="formId">
					<input id="contestId" name="contestId" th:value="${contestId}"  type="hidden">
					<div class="select-list">
						<ul>
							<li>
								题号：<input type="text" name="problemId" th:value="${problemId!=null?problemId:''}"/>
							</li>

							<li>
								用户名：<input type="text" name="uid"/>
							</li>
							<li>
								评判结果：<input type="text" id="verdict" name="verdict" th:value="${verdict!=null?verdict:1}"/>
							</li>
							<li>
								<a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
								<a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
							</li>
						</ul>
					</div>
				</form>
			</div>

			<div class="col-sm-12 select-table table-striped">
				<table id="bootstrap-table" data-mobile-responsive="true"></table>
			</div>
		</div>
	</div>
    <div th:include="include :: footer"></div>
    <script th:inline="javascript">
        var prefix = ctx + "acm/front";
        $(function() {
            var options = {
                url: prefix + "/listsolution",
                modalName: "提交状态",
                striped:true,
				search: false,
				showSearch:false,
				showRefresh:false,
				showColumns:false,
				showToggle:false,
				showExport:false,
				sortOrder:"desc",
				sortName:"submitDate",
				pageSize: 25,
                columns: [
          				{
        					field : 'solutionId', 
        					title : '编号'
        				},
				{
					field : 'submitDate', 
					title : '提交时间',
					sortable: true
				},
				{
					field : 'problemId', 
					title : '题号',
					formatter: function(value, row, index) {
						var actions = [];
                        actions.push('<a class="btn btn-success btn-xs" href="#" onclick="goproblem('+ row.problemId + ')" ><i class="fa fa-link"></i>'+value+'：'+row.title+'</a>  ');
                        return actions.join('');
					},
					sortable: true
				},
				{
					field : 'uid', 
					title : '用户名',
					sortable: true
				},
				{
					field : 'language', 
					title : '编程语言',
					formatter: function(value, row, index) {
						var lang="";
						switch(value) {
							case 3:lang= "GCC";break;
							case 4:lang= "G++";break;
							case 5:lang= "Java";break;
							case 9:lang= "Python3";
						}
						
						if([[${user.opensource}]]==false && ([[${user.id}]]!=""+row.uid+"" || row.contestId!=0)) return lang;
						else{
						var actions = [];
						actions.push('<a href="#" onclick="looksrc('+ row.solutionId +  ')"><i class="fa fa-eye"></i> '+lang+'</a> ');
                        return actions.join('');
						}
					}
				},
				{
					field : 'verdict', 
					title : '评判结果',
					formatter: function(value, row, index) {
						return viewverdict(row.solutionId,row.uid,value);
						
					}
				},
				{
					field : 'memory', 
					title : '内存'
				},
				{
					field : 'time', 
					title : '时间'
				},
				{
					field : 'codeLength', 
					title : '代码长度'
				}]
            };
            $.table.init(options);
            $.table.search();
            setTimeout(refreshstatus, 2000);
            //setInterval("refreshstatus()", 10000);
        });
        function refreshstatus(){//刷新整个table，效率不如刷新部分tr的td
        	//$("#verdict").val(1);
        	//$.table.search();
        	var a = new Array();
		 	var results = $('.verdict').each(function(i, el){
               var zz = $(el).attr('solid');
               a.push( zz );
      	 	});
		 	if( a.length > 0 ){	               
            	$.ajax({
                    type: "POST",
                    url: prefix + "/partsolution",
                    data: "ids="+a.join(','),
                    dataType: 'json',
                    success: function(data){
                    	for(var i = 0; i<data.length; i++){
                    		var row=data[i];                   		
                    		
                    	$(".verdict[solid="+row.solutionId+"]").parent().html(viewverdict(row.solutionId,row.uid,row.verdict));
                    	}
                    	setTimeout(refreshstatus, 5000);
                    }
            	})
		 	}
        }
        function viewverdict(solutionId,uid,value){
        	var verd="<span class='verdict' solid='"+ solutionId +"'>";
			var verd2="</span>";
			var lang="";
			switch(value) {
				case 1:
					lang='<a class="btn btn-info btn-xs" href="#"><i class="fa fa-spinner fa-spin"></i> 等待中...</a> ';
					lang=verd+lang+verd2;
					break;
				case 2:
					lang= "编译中...";
					lang=verd+lang+verd2;
					break;
				case 3:
					if([[${user.opensource}]]==true || [[${user.id}]]==""+uid+""){
					var actions = [];
					actions.push('<a class="btn btn-danger btn-xs" href="#" onclick="lookce('+ solutionId +  ')"><i class="fa fa-eye"></i> Compilation Error</a> ');
					return actions.join('');
					}else lang= "编译错误";
					break;
				case 4:
					lang= "运行中...";
					lang=verd+lang+verd2;
					break;
				case 5: 
					lang='<a class="btn btn-success btn-xs" href="#"><i class="fa fa-check"></i> 正确</a> ';
					break;
				case 6:
					lang='<a class="btn btn-danger btn-xs" href="#"><i class="fa fa-close"></i>答案错误</a> ';
					break;
				case 7:lang= "运行时错误";break;
				case 8:lang= "超时";break;
				case 9:lang= "内存超限";break;
				case 10:lang= "格式错误";break;
				case 11:lang= "输出超限";break;
				case 12:lang= "Restricted Function";break;
				case 13:lang= "Out Of Comtest";break;
				case 14:lang= "系统错误";break;
				case 15:lang= "Skipped";
			}
			return lang;
        }
        function lookce(solid){
        	$.modal.open2("错误信息", prefix + "/ce3?solutionId="+ solid );
        }
        function looksrc(solid){
        	$.modal.open2("源码信息", prefix + "/src?solutionId="+ solid );
        }
        function goproblem(pid){
        	var url = prefix + '/detail?problemId='+pid+ '&contestId='+[[${contestId}]];
			$.modal.openTab("题目详情", url);
			refreshItem();
        }
    </script>
</body>
</html>